<?php
/**
 * Created by PhpStorm.
 * ApiUserModel: xiao
 * Date: 2018/2/7
 * Time: 14:50
 */

namespace App\Http\Controllers\Org;


use App\Http\Common\Base\ApiController;
use App\Http\Common\Config\OpenConfig;
use App\Http\Common\Utils\JUtils;
use App\Http\Common\Utils\WeChat\WNet;
use Illuminate\Http\Request;

class WeChatController extends ApiController
{

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getOpenId(Request $request)
    {

        $openURL    = 'https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code';

        $code       = $request->input('code');

        $appID      = OpenConfig::WE_CHAT_APP_ID;
        $appSecret  = OpenConfig::WE_CHAT_APP_SECRET;

        $requestURL = sprintf($openURL, $appID, $appSecret, $code);

        $json       = WNet::G($requestURL);
        $jsonArr    = json_decode($json, true);

        if (!empty($jsonArr['errcode'])) return JUtils::RE($jsonArr['errmsg']);

        $returnData = array('session_key' => $jsonArr['session_key'], 'openId' => $jsonArr['openid']);

        return JUtils::R($returnData);
    }

}